package com.si.controller.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.si.entity.OperationLog;
import com.si.service.BlogrollService;
import com.si.service.OperationLogService;
import com.si.util.IpAddressUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.si.entity.Brand;
import com.si.service.BrandService;
import com.si.util.FileUpload;
@Controller
@RequestMapping("brand/manage")
public class BrandController {
	
	@Autowired
	private BrandService brandTwoService;

	@Autowired
	private OperationLogService operationLogService;
	/**
	 *  进入到列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="list")
	public String list(ModelMap modelMap,@ModelAttribute("message") String message){
		List<Brand> bannerList = brandTwoService.getAll();
		modelMap.put("bannerList", bannerList);
		modelMap.put("message", message);
		return "managePage/brand/list";
	}
	
	/**
	 * 进入到修改页面
	 * @param modelmap
	 * @return
	 */
	@RequestMapping(value="initUpdate/{id}")
	public String update(
			ModelMap modelMap,
			@PathVariable(value="id") Integer id){
		Brand banner=(Brand)brandTwoService.getObjectById(id);
		modelMap.put("banner", banner);
		return "managePage/brand/update";
	}
	
	/**
	 * 进入到添加页面
	 * @return
	 */
	@RequestMapping(value="initAdd")
	public String initAdd(ModelMap modelMap) {
		return "managePage/brand/update";
	}
	
	
	/**
	 * 保存数据
	 * @return
	 */
	@RequestMapping(value="save")
	public String save(HttpServletRequest request,
					   RedirectAttributes redirectAttributes,
					   ModelMap modelMap,
					   @RequestParam(value = "url") String url,
					   @RequestParam(value = "id") Integer id) {
		Brand banner=(Brand)brandTwoService.getObjectById(id);
		String image = FileUpload.picvideoupload("upload/brand", "fileInput", request);
		if (!org.apache.commons.lang3.StringUtils.isBlank(image)) {
			banner.setImage("upload/brand/" + image);
		}
		banner.setCreateDate(new Date());
		banner.setUrl(url);
		brandTwoService.saveOrUpdate(banner);
		OperationLog operationLog = new OperationLog();
		operationLog.setMiaoshu("执行成功！");
		operationLog.setOperationdetails("添加/修改申报系统-序号为"+banner.getId());
		operationLog.setOperationtime(new Date().toString());
		operationLog.setSensitive("申报系统");
		String ip = IpAddressUtil.getIpAddress(request);
		operationLog.setIpadderse(ip);
		String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
		operationLog.setUsername(sysUser);
		operationLogService.save(operationLog);
		redirectAttributes.addFlashAttribute("message", "信息保存成功");
		return "redirect:/brand/manage/list.html";
	}
	
	/**
	 * 根据id 删除
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="delete/{id}")
	public String delete(@PathVariable(value="id") Integer id,
											 HttpServletRequest request,	RedirectAttributes redirectAttributes){
		Brand banner=(Brand)brandTwoService.getObjectById(id);
		OperationLog operationLog = new OperationLog();
		operationLog.setMiaoshu("执行成功");
		operationLog.setOperationdetails("删除申报系统-序号为"+banner.getId());
		operationLog.setOperationtime(new Date().toString());
		operationLog.setSensitive("申报系统");
		String ip = IpAddressUtil.getIpAddress(request);
		operationLog.setIpadderse(ip);
		String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
		operationLog.setUsername(sysUser);
		operationLogService.save(operationLog);
		brandTwoService.delete(banner);
		redirectAttributes.addFlashAttribute("message", "信息删除成功");
		return "redirect:/brand/manage/list.html";

	}

}
